<?php
namespace BWL\webshopBundle\DataFixtures\ORM;
/**
 * Created by JetBrains PhpStorm.
 * User: leon
 * Date: 11/19/12
 * Time: 10:04 PM
 * To change this template use File | Settings | File Templates.
 */


use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use BWL\webshopBundle\Entity\User;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;


class UserFixtures extends AbstractFixture  implements OrderedFixtureInterface
{
  public function load(ObjectManager $manager)
  {
    $userLeon = new User();
    $userLeon->setFirstname('Leon');
    $userLeon->setLastname('Fausten');
    $userLeon->setUsername('leon');
    $userLeon->setEmail('leon.fausten@gmail.com');
    $userLeon->setSalt(md5(time()));
    $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
    $password = $encoder->encodePassword('1234', $userLeon->getSalt());
    $userLeon->setPassword($password);
    $userLeon->getUserRoles()->add($manager->merge($this->getReference('roleAdmin')));
    $manager->persist($userLeon);


    $userErwin = new User();
    $userErwin->setFirstname('Erwin');
    $userErwin->setLastname('Lang');
    $userErwin->setUsername('erwin');
    $userErwin->setEmail('erwin.lang@haw-hamburg.de');
    $userErwin->setSalt(md5(time()));
    $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
    $password = $encoder->encodePassword('1234', $userErwin->getSalt());
    $userErwin->setPassword($password);
    $userErwin->getUserRoles()->add($manager->merge($this->getReference('roleAdmin')));
    $manager->persist($userErwin);


    $userChristian = new User();
    $userChristian->setFirstname('Christian');
    $userChristian->setLastname('Ramcke');
    $userChristian->setUsername('christian');
    $userChristian->setEmail('eike-christian.ramcke@haw-hamburg.de');
    $userChristian->setSalt(md5(time()));
    $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
    $password = $encoder->encodePassword('1234', $userChristian->getSalt());
    $userChristian->setPassword($password);
    $userChristian->getUserRoles()->add($manager->merge($this->getReference('roleUser')));
    $manager->persist($userChristian);

    $userSven = new User();
    $userSven->setFirstname('Sven');
    $userSven->setLastname('Allers');
    $userSven->setUsername('sven');
    $userSven->setEmail('Sven.Allers@haw-hamburg.de');
    $userSven->setSalt(md5(time()));
    $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
    $password = $encoder->encodePassword('1234', $userSven->getSalt());
    $userSven->setPassword($password);
    $userSven->getUserRoles()->add($manager->merge($this->getReference('roleUser')));
    $manager->persist($userSven);

    $userJannik = new User();
    $userJannik->setFirstname('Jannik');
    $userJannik->setLastname('Graw');
    $userJannik->setUsername('jannik');
    $userJannik->setEmail('jannik.graw@haw-hamburg.de');
    $userJannik->setSalt(md5(time()));
    $encoder = new MessageDigestPasswordEncoder('sha512', true, 10);
    $password = $encoder->encodePassword('1234', $userJannik->getSalt());
    $userJannik->setPassword($password);
    $userJannik->getUserRoles()->add($manager->merge($this->getReference('roleUser')));
    $manager->persist($userJannik);

    $manager->flush();


    // Save references
    $this->addReference('userErwin', $userErwin);
    $this->addReference('userLeon', $userLeon);
    $this->addReference('userChristian', $userChristian);
    $this->addReference('userSven', $userSven);
    $this->addReference('userJannik', $userJannik);
  }

  public function getOrder() {
    return 3;
  }

}